package com.aiyige.model.commontag;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

public class CommonTag implements Parcelable {
    public static final int TYPE_ADD = 1;
    public static final int TYPE_NORMAL = 2;
    String id;
    String title;
    boolean selected;
    boolean edit;
    int type;

    public CommonTag() {
    }

    public CommonTag(CommonTag other) {
        this.id = other.id;
        this.title = other.title;
        this.selected = other.selected;
        this.edit = other.edit;
        this.type = other.type;
    }

    private CommonTag(Builder builder) {
        setId(builder.id);
        setTitle(builder.title);
        setSelected(builder.selected);
        setEdit(builder.edit);
        setType(builder.type);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public boolean isEdit() {
        return edit;
    }

    public void setEdit(boolean edit) {
        this.edit = edit;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public static final class Builder {
        private String id;
        private String title;
        private boolean selected;
        private boolean edit;
        private int type;

        private Builder() {
            id = "";
            title = "";
            selected = false;
            edit = false;
            type = TYPE_NORMAL;
        }

        public Builder id(String val) {
            id = val;
            return this;
        }

        public Builder title(String val) {
            title = val;
            return this;
        }

        public Builder selected(boolean val) {
            selected = val;
            return this;
        }

        public Builder edit(boolean val) {
            edit = val;
            return this;
        }

        public Builder type(int val) {
            type = val;
            return this;
        }

        public CommonTag build() {
            return new CommonTag(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.title);
        dest.writeByte(this.selected ? (byte) 1 : (byte) 0);
        dest.writeByte(this.edit ? (byte) 1 : (byte) 0);
        dest.writeInt(this.type);
    }

    protected CommonTag(Parcel in) {
        this.id = in.readString();
        this.title = in.readString();
        this.selected = in.readByte() != 0;
        this.edit = in.readByte() != 0;
        this.type = in.readInt();
    }

    public static final Parcelable.Creator<CommonTag> CREATOR = new Parcelable.Creator<CommonTag>() {
        @Override
        public CommonTag createFromParcel(Parcel source) {
            return new CommonTag(source);
        }

        @Override
        public CommonTag[] newArray(int size) {
            return new CommonTag[size];
        }
    };

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (this == obj) return true;
        if (!(obj instanceof CommonTag)) return false;
        CommonTag other = (CommonTag) obj;
        if (TextUtils.isEmpty(id) && TextUtils.isEmpty(other.id)) {
            return true;
        } else if (TextUtils.isEmpty(id) || TextUtils.isEmpty(other.id)) {
            return false;
        } else {
            return id.equals(other.id);
        }
    }
}
